package Package.Example.CollectionToFileDataSortingImprovements;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class CollectionToFileDataSortingImprovements {
    public static void CollectionToFileDataSortingImprovements() throws IOException {
        TreeSet<Student> ts = new TreeSet<>((o1, o2) -> {
            int num = o2.getSum() - o1.getSum();
            int num2 = num == 0 ? o1.getChinese() - o2.getChinese() : num;
            int num3 = num2 == 0 ? o1.getMath() - o2.getMath() : num;
            int num4 = num3 == 0 ? o1.getEnglish() - o2.getEnglish() : num;
            return num4;
        });
//        录入学生数据
        for (int i = 0; i < 5; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请录入第" + (i + 1) + "个学生信息：");
            System.out.println("姓名：");
            String name = sc.nextLine();
            System.out.println("语文成绩：");
            int chinese = sc.nextInt();
            System.out.println("数学成绩：");
            int math = sc.nextInt();
            System.out.println("英语成绩：");
            int english = sc.nextInt();
//创建学生对象，把键盘录入的数据对应赋值给学生对象的成员变量
            Student s = new Student();
            s.setName(name);
            s.setChinese(chinese);
            s.setMath(math);
            s.setEnglish(english);
//把学生对象添加到TreeSet集合
            ts.add(s);
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\insert\\student.txt"));
        for (Student s : ts) {
            bw.write(s.getName() + "," + s.getChinese() + "," + s.getMath() + "," + s.getEnglish() + "," + s.getSum());
            bw.newLine();
            bw.flush();
        }
        bw.close();

    }
}
